package com.woniuxy.woniuhis.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.woniuhis.entity.GodownItem;
import com.woniuxy.woniuhis.entity.OrderCart;
import com.woniuxy.woniuhis.entity.OrderList;
import com.woniuxy.woniuhis.entity.Pharmacy;
import com.woniuxy.woniuhis.entity.Repository;
import com.woniuxy.woniuhis.exception.OrderServiceException;
import com.woniuxy.woniuhis.mapper.GodownItemMapper;
import com.woniuxy.woniuhis.mapper.OrderCartMapper;
import com.woniuxy.woniuhis.mapper.OrderListMapper;
import com.woniuxy.woniuhis.mapper.PharmacyMapper;
import com.woniuxy.woniuhis.service.OrderService;
import com.woniuxy.woniuhis.service.RepositoryService;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	OrderCartMapper orderCartMapper;

	@Autowired
	PharmacyMapper pharmacyMapper;

	@Autowired
	GodownItemMapper godownItemMapper;

	@Autowired
	OrderListMapper orderListMapper;

	@Autowired
	RepositoryService repositoryService;

	/*
	 * 生成一张购物单的方法
	 */
	@Transactional // 需要事务
	@Override
	public List<OrderList> findAllOrderList() {

		try {
			List<OrderList> allList = orderListMapper.selectAllOrderList();

			return allList;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("findAllOrderList异常");
			throw new OrderServiceException("findAllOrderList异常");
		}

	}

	/*
	 * 生成一张购物单的方法
	 */
	@Transactional // 需要事务
	@Override
	public void addOrder(String pharId, String empName) {

		try {

			// 查询到购物车的情况 存入数组
			List<OrderCart> orderCarts = orderCartMapper.selectOrderCart();
			// 创建一个订单 前端输入药房id 当前人物
			OrderList orderList = new OrderList();
			orderList.setListId(UUID.randomUUID().toString());
			orderList.setPharId(pharId);
			orderList.setListOrderTime(new Date());
			// 总价
			Double total = 0.0;
			// 支付状态
			orderList.setPayState(false);
			orderList.setEmpName(empName);
			// 填充订单条目
			List<GodownItem> godownItems = new ArrayList<GodownItem>();
			for (int i = 0; i < orderCarts.size(); i++) {
				GodownItem godownItem = new GodownItem();
				godownItem.setItemId(UUID.randomUUID().toString());
				godownItem.setPharId(pharId);
				// 找到药房名字
				Pharmacy realityPharmacy = pharmacyMapper.selectPharmacyByPharmacyId(new Pharmacy(pharId, "?"));
				godownItem.setPharName(realityPharmacy.getPharName());
				// 药物相关消息
				godownItem.setMediId(orderCarts.get(i).getMediId());
				godownItem.setMediName(orderCarts.get(i).getMediName());
				godownItem.setMediCost(orderCarts.get(i).getMediCost());
				godownItem.setMediAmount(orderCarts.get(i).getMediAmount());
				// 生产商
				godownItem.setManuName(orderCarts.get(i).getManuName());
				// 经办人
				godownItem.setEmpName(empName);
				// 小计
				godownItem.setSubtotal(orderCarts.get(i).getSubtotal());
				// 获取listid
				godownItem.setListId(orderList.getListId());

				// 存入数据库
				godownItemMapper.insertGodownItem(godownItem);
				// 充填进去
				godownItems.add(godownItem);
				// 计算总价
				total = total + orderCarts.get(i).getSubtotal();

				// 修改储量 根据诊所id和药物id来增加储量
				Repository addre = new Repository();
				addre.setMediId(orderCarts.get(i).getMediId());
				addre.setPharId(pharId);
				addre.setStock(orderCarts.get(i).getMediAmount());
				repositoryService.updateRepositoryByPharIdAndMediId(addre);

			}
			// 充填订单的总价
			orderList.setTotalPrice(total);
			// 把订单存入数据库
			orderListMapper.insertOrderList(orderList);

			// 删除购物车
			orderCartMapper.deleteAllOrderCart();

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("addOrder异常");
			throw new OrderServiceException("addOrder异常");
		}

	}

}
